package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishServices;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品控制器
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
@Slf4j
public class DishControllers {

    @Autowired
    private DishServices dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品(day03任务二)
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("[新增菜品] dishDto:{}", dishDTO);
        dishService.addDish(dishDTO);
        //新增菜品后,需要情况缓存
        String key = "dish:" + dishDTO.getCategoryId();
        clearRedisByCategoryId(key);
        return Result.success();
    }

    /**
     * 菜品分页查询(day03任务三)
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页获取菜品")
    public Result<PageResult> findDishPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("[获取菜品列表] dishPageQueryDTO:{}", dishPageQueryDTO);

        PageResult pageResult = dishService.findDishPage(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 删除菜品(day03任务四)
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids) {
        log.info("[批量删除] ids:{}", ids);
        dishService.deleteDishByIds(ids);
        //全部删除
        clearRedisByCategoryId("dish:*");
        return Result.success();
    }

    /**
     * 修改菜品(day03任务5)
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("[更新菜品] dishDto:{}", dishDTO);
        dishService.updateDishById(dishDTO);
        //缓存菜品(day07任务一)
        clearRedisByCategoryId("dish:" + dishDTO.getCategoryId());
        return Result.success();
    }

    //缓存菜品公共方法(day07任务一)
    private void clearRedisByCategoryId(String keyPatten) {
        //根据key去redis搜索相关的key
        Set<String> keys =redisTemplate.keys(keyPatten);
        if (keys!=null){
            redisTemplate.delete(keys);
        }
    }

    /**
     * 修改菜品，首先回显(day03任务5)
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID回显菜品")
    public Result<DishVO> findById(@PathVariable Long id) {
        log.info("[基于ID查询] id:{}", id);
        DishVO dishVO = dishService.findById(id);
        return Result.success(dishVO);
    }


    /**
     * 根据分类ID查询菜品(day04任务一)
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类ID查询菜品")
    public Result<List<Dish>> findDishById(Long categoryId) {
        log.info("[基于ID分类查询菜品] categoryId:{}", categoryId);
        List<Dish> list = dishService.findDishById(categoryId);
        return Result.success(list);

    }
}
